package com.miracle9.game.service;

import com.miracle9.game.util.MyUtil;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;
import org.apache.mina.core.buffer.IoBuffer;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;

public class AmfMessageSend {

    public static void send(Data data) {
        try {
            HashMap<String, Object> map = new HashMap();
            map.put("method", data.method);
            map.put("args", data.result);
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            Amf3Output amf3Output = new Amf3Output(SerializationContext.getSerializationContext());
            amf3Output.setOutputStream(bout);
            amf3Output.writeObject(map);
            amf3Output.flush();
            amf3Output.close();

            byte[] content_out = bout.toByteArray();
            content_out = MyUtil.Base64Encode_session(content_out, data.session);
            IoBuffer bb = IoBuffer.allocate(content_out.length + 4);
            bb.putInt(content_out.length);
            bb.put(content_out);
            bb.flip();

            data.session.write(bb);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("send(Data data)失败");
        }
    }

}
